import App from './App'
import https from 'api/https.js'
import { getUserId } from 'utils/common.js'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
import uView from './uni_modules/uview-ui'
Vue.use(uView)
Vue.prototype.https = https;//自定义请求封装
Vue.prototype.getUserId = getUserId;//自定义请求封装
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif


Vue.prototype.go = function (url, location) {
	console.log("url ---- ",url)
	if(url.includes('/subPackages/classify/goodsDetail/goodsDetail')){
		uni.navigateTo({
			url: url,
		});
	}else if(!uni.getStorageSync('token')){
		// 判断当前页面栈里是否已有登录页
		const pages = getCurrentPages();
		const hasLogin = pages.some(p => p.route === 'subPackages/mine/login/login');
		if (!hasLogin) {
		  uni.navigateTo({
		  	url: '/subPackages/mine/login/login'
		  })
		}
	}else{
		uni.navigateTo({
			url: url,
		});
	}
};


Vue.prototype.toast = function (msg) {
	uni.showToast({
		icon: "none",
		title: msg,
	});
};


Vue.prototype.noOpen = function (msg) {
	uni.showToast({
		icon: "none",
		title: '暂未开放',
	});
};

// 全局购物车数量管理
Vue.prototype.updateCartBadge = function (count) {
	if (count > 0) {
		uni.setTabBarBadge({
			index: 3, // 购物车选项卡索引
			text: count.toString()
		});
	} else {
		uni.removeTabBarBadge({
			index: 3
		});
	}
};

// 获取购物车数量
Vue.prototype.getCartCount = function () {
	return uni.getStorageSync('cartCount') || 0;
};

// 设置购物车数量
Vue.prototype.setCartCount = function (count) {
	uni.setStorageSync('cartCount', count);
	this.updateCartBadge(count);
};

// 增加购物车数量
Vue.prototype.addCartCount = function (num = 1) {
	const currentCount = this.getCartCount();
	const newCount = currentCount + num;
	this.setCartCount(newCount);
};

// 减少购物车数量
Vue.prototype.reduceCartCount = function (num = 1) {
	const currentCount = this.getCartCount();
	const newCount = Math.max(0, currentCount - num);
	this.setCartCount(newCount);
};

// 清空购物车数量
Vue.prototype.clearCartCount = function () {
	this.setCartCount(0);
};

